<?php
/*
mae. 1.0
Some additional functions
*/

/* checkIfExists( string $module )
 * Checks if specified $module exists, if not returns false
 */
function checkIfExists($module) {
	global $db;
	$paths = array();
	$xml = simplexml_load_file('internal/modulepaths.xml');
	foreach ($xml->module as $mod) $paths[] = (string)$mod->path.'/';

	if (empty($module)) error('admin/errors/nomodule');

	// First pass checking: the directory
	for ($i=0; $i<count($paths); $i++) {
		if (file_exists($paths[$i].$module."/")) $works = $paths[$i]."$module/";
	}
	if (!$works) return false;

	// Second pass checking: the database
	$module_s = addslashes($module);
	$db->modeNum();
	$q = $db->select('modules', 'COUNT(*)', array("shortname='$module_s'"));
	if ($q->fields[0] > 0) return $works;

	return false;
}

/* error( string $msg )
 * Reroute page to the error.php script with specified error message
 */
function error($msg) {
	$redir = $_SERVER['PHP_SELF'];
	echo "<script>window.location = '{$mae[site_url]}error.php?code=PHP&error=$msg&redir=$redir';</script>";
	exit;
}

/*
 function engine_type(string $engine)
 Returns the engine's type.
*/
function engine_type($engine) {
	global $db;
	$db->modeNum();
	$q = $db->select("modules", 'type', array("shortname='$engine'"));
	$r = $q->fields;
	return substr($r[0], 0, strpos($r[0],'engine'));
}
?>
